package com.itheima.recyclerviewdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * @author Administrator
 * @time 2015/8/4 16:36
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDate $Date$
 * @updateDes ${TODO}
 */
public class StraggerAdapter extends RecyclerView.Adapter<StraggerAdapter.MyHolder> {
    private Context context;
    private List<DataBean> datas;

    public StraggerAdapter(Context context, List<DataBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {//决定根布局
        //        TextView tv = new TextView(context);//根布局
        View itemView = View.inflate(context, R.layout.item_stragger, null);
        return new MyHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyHolder myHolder, int position) {//填充数据
        myHolder.setDataAndRefreshUI(datas.get(position));
    }

    @Override
    public int getItemCount() {//条目总数
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        //孩子对象
        private TextView mTvName;
        private ImageView mIvIcon;

        public MyHolder(View itemView) {
            super(itemView);
            //初始化孩子对象
            mTvName = (TextView) itemView.findViewById(R.id.item_straggered_tv);
            mIvIcon = (ImageView) itemView.findViewById(R.id.item_straggered_iv);
        }

        /**
         * 设置itemView的数据展示
         *
         * @param dataBean
         */
        public void setDataAndRefreshUI(DataBean dataBean) {
            mTvName.setText(dataBean.text);
            mIvIcon.setImageResource(dataBean.iconId);
        }
    }
}
